package p3.cse;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * This activity is called from unlocked screen for a user to change the main
 * login password.
 * 
 * @see /res/layout/changepassword.xml
 */
public final class Changepassword extends Activity {
	/**
	 * The event listener for user confirming the change of their password.
	 * Verifies the old password was correct and that the new password fields
	 * match and meets the criteria for valid password defined in
	 * Filer.checkStrength()
	 * 
	 * @see Filer.java
	 * @see /res/layout/changepassword.xml
	 */
	private OnClickListener changePassword = new OnClickListener() {
		public void onClick(final View v) {
			Log.d("ok button", "clicked");
			// warning text box
			TextView warning = (TextView) findViewById(R.id.passwordwarning);
			warning.setText(" ");
			warning.setTextColor(Color.RED);
			// get form field entries
			String old = ((EditText) findViewById(R.id.oldpasswordinput))
					.getText().toString();
			String new1 = ((EditText) findViewById(R.id.newpassword1))
					.getText().toString();
			String new2 = ((EditText) findViewById(R.id.newpassword2))
					.getText().toString();

			// does new password = new password 2?
			if (!new1.equals(new2)) {
				warning.setText("New passwords do not match.");
				warning.setVisibility(1);

				// new password entries did match
			} else if (old.equals(new1)) {
				warning.setText("New password must be different than the old one.");
				warning.setVisibility(1);

				// new password entries did match
			} else {
				String securityCheck = Filer.checkStrength(new1);

				// new password is good
				if (securityCheck == null) {
					Filer filer = Filer.getInstance();

					// failed to update the password
					if (!filer.changePassword(old, new1)) {

						warning.setText("Could not change password, exit "
								+ "application and try again.");
						warning.setVisibility(1);
					} else {
						try {
							filer.writeFile();
						} catch (IOException e) {
							Log.e("ChangePassword",
									"Error writing password database", e);
						}

						finish();
					}
				} else {
					warning.setText(securityCheck);
					warning.setVisibility(1);
				}
			}
		}

	};

	/**
	 * The default constructor called by Android. This sets up the activity and
	 * launches the changepassword.xml layout.
	 * 
	 * {@inheritDoc}
	 * 
	 * @see /res/layout/changepassword.xml
	 */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.changepassword);
		Button ok = (Button) findViewById(R.id.acceptnewpasswordButton);
		ok.setOnClickListener(changePassword);
	}
}
